package com.sky.financelake.ui.fragment.community;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.snackbar.Snackbar;
import com.sky.financelake.R;
import com.sky.financelake.adapter.BaseAdapter;
import com.sky.financelake.adapter.CommunityNewsAdapter;
import com.sky.financelake.base.BaseF;
import com.sky.financelake.bean.dto.CommunityDto;
import com.sky.financelake.databinding.FragmentCommunityNewsBinding;
import com.sky.financelake.ui.activity.CommunityDetailA;
import com.sky.financelake.ui.wigets.EndlessRecyclerOnScrollListener;
import com.sky.financelake.utils.LoadingUtil;
import com.sky.financelake.viewmodels.remote.CommunityViewModel;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class NewsF extends BaseF {

    private FragmentCommunityNewsBinding binding;
    private CommunityNewsAdapter mNewsAdapter;
    private LoadingUtil loadingUtil;
    private CommunityViewModel mCommunityViewModel;
    private Integer curPage = 1;
    private List<CommunityDto> communityDtos = new ArrayList<>();
    private SwipeRefreshLayout refreshLayout;


    public NewsF() {
    }

    public static NewsF newInstance() {
        NewsF skillF = new NewsF();
        return skillF;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        binding = FragmentCommunityNewsBinding.inflate(getLayoutInflater());
        loadingUtil = LoadingUtil.newInstance(binding.getRoot());

        mCommunityViewModel = new ViewModelProvider(requireActivity()).get(CommunityViewModel.class);


        refreshLayout = binding.refreshLayout;
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                curPage = 1;
                communityDtos.clear();
                load();
            }
        });

        final RecyclerView rvNews = binding.rvNews;

        mNewsAdapter = new CommunityNewsAdapter(requireActivity(), communityDtos);
        mNewsAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onClick(Integer position, Object o) {
                CommunityDto communityDto = (CommunityDto) o;
                Intent intent = new Intent(requireActivity(), CommunityDetailA.class);
                intent.putExtra("id", communityDto.id);
                intent.putExtra("title", communityDto.title);
                startActivity(intent);
            }
        });
        rvNews.setLayoutManager(new LinearLayoutManager(getContext()));
        rvNews.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                Log.i(TAG, "onLoadMore: 加载");
                if (mNewsAdapter.getStatus() != BaseAdapter.LOADING_END) {
                    load();
                }
            }
        });
        rvNews.setAdapter(mNewsAdapter);
        load();
        return binding.getRoot();
    }

    public void load() {
        mCommunityViewModel.getCommunityPagingList(0, curPage).observe(this, body -> {
            if (refreshLayout.isRefreshing()){
                refreshLayout.setRefreshing(false);
            }
            if (body != null) {
                List<CommunityDto> tmp = (List<CommunityDto>) body.getResult();
                if (tmp.size() == 0) {
                    mNewsAdapter.setStatus(BaseAdapter.LOADING_END);
                    mNewsAdapter.notifyItemChanged(communityDtos.size());
                } else {
                    curPage++;
                    communityDtos.addAll(tmp);
                    mNewsAdapter.notifyItemRangeChanged(communityDtos.size(),tmp.size());
                }
            } else {
                Snackbar.make(refreshLayout, R.string.api_error, 500);
            }

        });
    }

}
